package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//医院设置接口
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation(value = "模拟登陆")
    @PostMapping("login")

    // {"code":20000,"data":{"token":"admin-token"}}
    public R login() {
        return R.ok().data("token", "admin-token");
    }

    @ApiOperation(value = "模拟获取用户信息")
    @GetMapping("info")
    //{"code":20000,"data":{"roles":["admin"],
    //"introduction":"I am a super administrator",
    //"avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    //"name":"Super Admin"}}
    public R info() {
        Map<String, Object> map = new HashMap<>();
        map.put("roles", "admin");
        map.put("introduction", "I am a super administrator");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name", "Super Admin");
        return R.ok().data(map);
    }

    //查询所有医院设置
    // @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public R findAll() {

        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping("delete/{id}")
    public R deleteById(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页查询医院设置")
    @GetMapping("pageList/{page}/{limit}")
    public R pageList(@PathVariable Long page, @PathVariable Long limit) {
        Page<HospitalSet> pageParams = new Page<>(page, limit);

        Page<HospitalSet> pageModel = hospitalSetService.page(pageParams);
        List<HospitalSet> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("records", records).data("total", total);
    }

    @ApiOperation(value = "分页条件查询医院设置")
    @PostMapping("pageQuery/{page}/{limit}")
    public R pageQuery(@PathVariable Long page, @PathVariable Long limit, @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> pageParams = new Page<>(page, limit);
        Page<HospitalSet> pageModel = null;
        if (hospitalSetQueryVo == null) {
            pageModel = hospitalSetService.page(pageParams);
        } else {
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();
            if (!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname", hosname);
            }
            if (!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode", hoscode);
            }
            pageModel = hospitalSetService.page(pageParams, wrapper);
        }

        List<HospitalSet> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("records", records).data("total", total);
    }

    @ApiOperation(value = "新增医院设置")
    @PostMapping("save")
    public R save(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.save(hospitalSet);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet != null) {
            return R.ok().data("hospitalSet", hospitalSet);
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改医院设置")
    @PostMapping("update")
    public R update(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean b = hospitalSetService.removeByIds(idList);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}